/*
 * @(#)Options.java        0.1 09/04/29
 * @version 	0.1 29 Apr 2009
 * @author 	Nguyen Thang Huy
 * 
 * Mo va luu tru cac tuy chon cua nguoi dung dang XML
 */


package XML_IO;

import Data.*;
import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import org.xml.sax.*;

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class Options {
	private String optionsFileName = "options";
	private LinkedList<Theme> themes;
	private LinkedList<Language> languages;
	
	/*
	 * Doc cac tuy chon tu file XML
	 */
	public Options() {
		themes = new LinkedList<Theme>();
		languages = new LinkedList<Language>();
		Document doc = parseFile(new File(optionsFileName));
		Node root = doc.getDocumentElement();
		NodeList children = root.getChildNodes();
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			
			if (child.getNodeType() == Node.ELEMENT_NODE) {
				// Doc cac theme
				if (child.getNodeName().equals("themes")) {
					NodeList tmp = child.getChildNodes();
					for (int j = 0; j < tmp.getLength(); j++) {
						if (tmp.item(j).getNodeType() == Node.ELEMENT_NODE) {
							Theme t = new Theme(tmp.item(j));
							themes.addLast(t);
						}
					}
				}
			
				// Doc Danh sach cac ngon ngu
				if (child.getNodeName().equals("languages")) {
					NodeList tmp = child.getChildNodes();
					for (int j = 0; j < tmp.getLength(); j++) {
						if (tmp.item(j).getNodeType() == Node.ELEMENT_NODE) {
							Language l = new Language(tmp.item(j));
							languages.addLast(l);
						}
					}
				}
			}
		}
	}
	
	/*
	 * Mo file XML
	 */
    private Document parseFile (File file) {
    	DocumentBuilder docbuilder;
    	Document doc = null;
    	DocumentBuilderFactory docbuilderfactory = DocumentBuilderFactory.newInstance();
    	docbuilderfactory.setIgnoringElementContentWhitespace(true);
    	try {
    		docbuilder = docbuilderfactory.newDocumentBuilder();
    	} catch (ParserConfigurationException e){
    		System.out.println("wrong parser configuration: "+e.getMessage());
    		return null;
    	}
    	try {
    		doc = docbuilder.parse(file);
    	} catch (SAXException e){
    		System.out.println("wrong XML file structure: "+e.getMessage());
    		return null;
    	}catch (IOException e){
    		System.out.println("Could not read source file: "+e.getMessage());
    	}
    	System.out.println("parse XML file");
    	return doc;
    }
    
    public LinkedList<Theme> getThemes() {
    	return themes;
    }
    
    public LinkedList<Language> getLanguages() {
    	return languages;
    }
    
    /*
     * Luu cac tuy chon thanh file XML
     */
    public void save(LinkedList<Language> l, LinkedList<Theme> t) {
    	DocumentBuilder docbuilder;
    	DocumentBuilderFactory docbuilderfactory = DocumentBuilderFactory.newInstance();
    	docbuilderfactory.setIgnoringElementContentWhitespace(true);
    	try {
    		docbuilder = docbuilderfactory.newDocumentBuilder();
    	} catch (ParserConfigurationException e){
    		System.out.println("wrong parser configuration: "+e.getMessage());
    		return;
    	}
    	Document doc = docbuilder.newDocument();
    	Element root = doc.createElement("JCG");
    	root.setAttribute("type", "Options");
    	doc.appendChild(root);
    	
    	//Luu cac language
    	Element languagesElement = doc.createElement("languages");
    	root.appendChild(languagesElement);
    	for (int i = 0; i < l.size(); i++) {
    		languagesElement.appendChild(l.get(i).toXml(doc));
    	}
    	
    	// Luu cac theme
    	Element themesElement = doc.createElement("themes");
    	root.appendChild(themesElement);
    	for (int i = 0; i < t.size(); i++) {
    		themesElement.appendChild(t.get(i).toXml(doc));
    	}
    	
    	TransformerFactory transformerFactory = TransformerFactory.newInstance();
    	Transformer transformer = null;
    	try {
    		transformer = transformerFactory.newTransformer();    		
    	} catch (Exception e) {
    		
    	}
        
        DOMSource source = new DOMSource(doc);
        File file = new File(optionsFileName);
        //File file = new File(getClass().getResource("options").toString());
        StreamResult result =  new StreamResult(file);
        try {
        	transformer.transform(source, result);
        } catch (Exception e) {
        	
        }
    }
}
